﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Services;
using System.Net.Sockets;
using System.Runtime.Remoting.Channels.Http;

namespace MisServer
{
    public partial class FrmServer : Form
    {

        private TcpChannel tcp;

        public FrmServer()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (StartServer())
            {
                this.btnStart.Enabled = false;
                this.btnStop.Enabled = true;
            }
        }

        private void FrmServer_Load(object sender, EventArgs e)
        {
            //读取配置信息
            this.txtServerAddr.Text = System.Configuration.ConfigurationManager.AppSettings["ServerAddr"];
            this.txtServerPort.Text = System.Configuration.ConfigurationManager.AppSettings["ServerPort"];
            this.txtDbName.Text = System.Configuration.ConfigurationManager.AppSettings["DbName"];
            this.txtDbUser.Text = System.Configuration.ConfigurationManager.AppSettings["DbUser"];
            this.txtDbPwd.Text = System.Configuration.ConfigurationManager.AppSettings["DbPwd"];
            this.btnStop.Enabled = false;
        }

        private bool StartServer()
        {
            try
            {
                //新建端口号，tcp的
                this.tcp = new TcpChannel(int.Parse(this.txtServerPort.Text.Trim()));
            }
            catch (SocketException e)
            {
                MessageBox.Show(e.Message.ToString(), "运行时错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtServerPort.Focus();
                return false;
            }
            try
            {
                //注册信道
                ChannelServices.RegisterChannel(tcp, false);

                Type tpPos = typeof(MisBLL.PosDAO);
                Type tpOper = typeof(MisBLL.OperDAO);
                Type tpType = typeof(MisBLL.TypeDAO);
                Type tpDepart = typeof(MisBLL.DepartDAO);
                Type tpArea = typeof(MisBLL.AreaDAO);
                Type tpCard = typeof(MisBLL.CardDAO);
                Type tpCusShop = typeof(MisBLL.CusShopDAO);
                Type tpDayJS = typeof(MisBLL.DayJSDAO);
                Type tpFkType = typeof(MisBLL.FkTypeDAO);
                Type tpJhType = typeof(MisBLL.JhTypeDAO);
                Type tpUnit = typeof(MisBLL.UnitDAO);
                Type tpYt = typeof(MisBLL.YTDAO);
                Type tpCusDep = typeof(MisBLL.CusDepDAO);
                Type tpSupply = typeof(MisBLL.SupplyDAO);
                Type tpGoods = typeof(MisBLL.GoodsDAO);
                Type tpSys0 = typeof(MisBLL.Sys0DAO);
                Type tpBacf = typeof(MisBLL.BacfDAO);
                Type tpRacf = typeof(MisBLL.RacfDAO);
                Type tpShop = typeof(MisBLL.ShopDAO);
                Type tpCustomer = typeof(MisBLL.CustomerDAO);
                Type tpGoodsCus = typeof(MisBLL.GoodsCusDAO);
                Type tpSacf=typeof(MisBLL.SacfDAO);



                //将服务器上的对象注册为已知实例
                RemotingConfiguration.RegisterWellKnownServiceType(tpPos, "PosDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpOper, "OperDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpType, "TypeDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpDepart, "DepartDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpArea, "AreaDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpCard, "CardDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpCusShop, "CusShopDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpDayJS, "DayJSDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpFkType, "FkTypeDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpJhType, "JhTypeDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpUnit, "UnitDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpYt, "YTDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpCusDep, "CusDepDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpSupply, "SupplyDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpGoods, "GoodsDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpSys0, "Sys0DAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpBacf, "BacfDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpRacf, "RacfDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpShop, "ShopDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpCustomer, "CustomerDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpGoodsCus, "GoodsCusDAO", WellKnownObjectMode.SingleCall);
                RemotingConfiguration.RegisterWellKnownServiceType(tpSacf, "SacfDAO", WellKnownObjectMode.SingleCall);
            }
            catch (Exception ex)//捕获异常
            {
                MessageBox.Show(ex.Message);//弹出异常
                return false;
            }
            return true;
        }


        private void CloseServer()
        {
            //注销信道
            ChannelServices.UnregisterChannel(tcp);
            //端口停止侦听
            tcp.StopListening(null);    
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            CloseServer();
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
        }

        private void FrmServer_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.notifyIcon1.Visible = true;
                this.ShowInTaskbar = false;
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
            }
        }
    }
}
